<?php

/**
 * Class AdminController
 */
class AdminController extends BaseController
{

	/*
	|--------------------------------------------------------------------------
	| 后台控制器
	|--------------------------------------------------------------------------
	|
	|	dashboard	    仪表盘
	|	GetCaptcha		验证码
	|	DisposeLogin	登陆验证
	|	Logout			注销用户
	|	GetStuffList	员工列表
	|	GetStuffListPart员工列表分页
	|	displayStuffList员工列表模版分发
	|
	*/

    /**
     * 仪表盘
     * @return mixed
     * @author buluota
     * @date 2016-03-05
     */
    public function Dashboard()
    {
        return Redirect::to('/manage_product');
    }

    /**
     * 登录页面
     * @return mixed
     * @author buluota
     * @date 2016-03-05
     */
    public function Login()
    {
        return $this->view('admin.login');
    }

    /**
     * 登录请求
     * @return mixed
     * @author buluota
     * @date 2016-03-05
     */
    public function DisposeLogin()
    {
        #接受参数
        $paramObj                   =          new stdClass();
        $paramObj->username         =          $this->getParam('username');          //用户名
        $paramObj->password         =          $this->getParam('password');          //密码
        $paramObj->captcha          =          $this->getParam('captcha');           //验证码

        try{
            AdminService::instance()->LoginValidator($paramObj);
            return Redirect::to('/show_admin');
        }catch (Exception $e){
            return Redirect::to('/login');
        }
    }

    /**
     * 生成验证码
     * @author buluota
     * @date 2016-03-05
     */
    public function Captcha()
    {
        CaptchaService::instance()->create();
    }

    /**
     * 注销用户
     * @return mixed
     * @author buluota
     * @date 2016-03-05
     */
    public function Logout()
    {
        if(Session::has(UserEnum::USER_INFO_SESSION_KEY)) {
            Session::flush();
        }
        return Redirect::to('/login');
    }

    /**
     * 管理员管理
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ManageAdmin()
    {
        $data = AdminService::instance()->ManageAdmin();
        return $this->view('admin.manage_admin')->with('admin_list', $data);
    }

    /**
     * 添加管理员
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function AddAdmin()
    {
        return $this->view('admin.add_admin');
//        if(Request::isMethod('POST'))
//        {
//            #接受参数
//            $aData              =   new stdClass();
//            $aData->name        =   $this->getParam('name');
//            $aData->real_name   =   $this->getParam('real_name');
//            $aData->password    =   $this->getparam('password');
//            $aData->avatar      =   $this->getParam('avatar');
//            $aData->role_id     =   $this->getParam('role_id');
//            $aData->mobile      =   $this->getparam('mobile');
//            $aData->status       =   $this->getparam('status');
//            $aData->created     =   time();
//
//            $check =  $this->checkProperties($aData,['name','real_name','avatar','role_id','password','mobile','status','created']);
//            if(!$check){
//                $this->rest->error('传参为空!',ErrorCodeEnum::STATUS_ERROR,$aData);}
//            AdminService::instance()->addAdminInfo($aData);
//            return Redirect::to('/show_admin');}else{
//            return $this->view('admin.add_admin');
//        }
    }

    /**
     * 管理员角色管理
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ManageAdminRole()
    {
        return $this->view('admin.manage_admin_role');
    }

    /**
     * 添加管理员角色
     * @return mixed
     * @author buluota
     * @data 2016-03-06
     */
    public function AddAdminRole()
    {
        return $this->view('admin.add_admin_role');
    }

    /**
     * 商家管理
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ManageSupplier()
    {
        $suppliers = AdminService::instance()->GetSupplierList();
        return $this->view('admin.manage_supplier')->with(['suppliers' => $suppliers]);
    }

    /**
     * 商家入驻
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function AddSupplier()
    {
        return $this->view('admin.add_supplier');
    }

    /**
     * 商家入库
     * @return mixed
     * @author buluota
     * @date 2016-03-09
     */
    public function SupplierImportAction()
    {
        #接受参数
        $paramObj                       =          new stdClass();
        $paramObj->supplier_name        =          $this->getParam('supplier_name');          //商家名称
        $paramObj->owner                =          $this->getParam('owner');                  //法人
        $paramObj->company              =          $this->getParam('company');                //公司名称
        $paramObj->address              =          $this->getParam('address');                //公司地址
        $paramObj->brand                =          $this->getParam('brand');                  //所属品牌
        $paramObj->scale                =          $this->getParam('scale');                  //公司规模
        $paramObj->phone                =          $this->getParam('phone');                  //联系电话
        $paramObj->business_scope       =          $this->getParam('business_scope');         //经营范围
        $paramObj->id_card              =          $this->getParam('id_card');                //身份证
        $paramObj->bank_account         =          $this->getParam('bank_account');           //账户主体
        $paramObj->bank_number          =          $this->getParam('bank_number');            //对公账户
        $paramObj->operate_license      =          $this->getParam('operate_license');        //营业执照图
        $paramObj->tax_license          =          $this->getParam('tax_license');            //税务登记证
        $paramObj->show_img1            =          $this->getParam('show_img1');              //商家图片1
        $paramObj->show_img2            =          $this->getParam('show_img2');              //商家图片2
        $paramObj->show_img3            =          $this->getParam('show_img3');              //商家图片3

        $paramObj->market               =          $this->getParam('market');                 //木材市场
        $paramObj->feature              =          $this->getParam('feature');                //服务特色
        $paramObj->storage              =          $this->getParam('storage');                //仓储地
        $paramObj->brief                =          $this->getParam('brief');                  //公司介绍
        $paramObj->is_foreign           =          $this->getParam('is_foreign');             //国外商家

        #逻辑处理
        try{
            AdminService::instance()->ImportSupplier($paramObj);
            return Redirect::to('/manage_supplier');
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode());
        }

    }

    /**
     * 资讯列表
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function NewsList()
    {
        
        return $this->view('admin.news_list');
    }

    /**
     * 资讯导入
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function NewsImport()
    {
        return $this->view('admin.news_import');
    }

    /**
     * 会员管理
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ManageCustomer()
    {
        return $this->view('admin.manage_customer');
    }

    /**
     * 马甲列表
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function MajiaList()
    {
        return $this->view('admin.majia_list');
    }

    /**
     * 会员录入
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function CustomerImport()
    {
        return $this->view('admin.customer_import');
    }

    /**
     * 林场服务
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ForestService()
    {

        return $this->view('admin.forest_service');
    }

    /**
     * 物流服务
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function TransportService()
    {
        return $this->view('admin.transport_service');
    }

    /**
     * 仓储服务
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function StorageService()
    {
        return $this->view('admin.storage_service');
    }

    /**
     * 港口服务
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function HarbourService()
    {
        return $this->view('admin.harbour_service');
    }

    /**
     * 林场录入
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ForestImport()
    {
        return $this->view('admin.forest_import');
    }

    /**
     * 物流录入
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function TransportImport()
    {
        return $this->view('admin.transport_import');
    }

    /**
     * 仓储录入
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function StorageImport()
    {
        return $this->view('admin.storage_import');
    }

    /**
     * 港口录入
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function HarbourImport()
    {
        return $this->view('admin.harbour_import');
    }

    /**
     * 产品管理
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ManageProduct()
    {
        $product_list = AdminService::instance()->GetProductBoard();
        return $this->view('admin.manage_product')->with(['product_list' => $product_list]);
    }

    /**
     * 产地管理
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ProductPlace()
    {
        $place = AdminService::instance()->GetPlaceList();
        return $this->view('admin.product_place')->with(['place' => $place]);
    }

    /**
     * 品牌管理
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ProductBrand()
    {
        $brand = AdminService::instance()->GetBrandList();
        return $this->view('admin.product_brand')->with(['brand' => $brand]);
    }

    /**
     * 分类管理
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ProductCate()
    {
        return $this->view('admin.product_cate');
    }

    /**
     * 产品录入
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ProductImport()
    {
        $product                =   new stdClass();
        $product->brand         =   GoodsService::instance()->ShowGoodsBrandList();   //品牌
        $product->quality       =   GoodsService::instance()->GetProductCateList();   //材质
        $product->channel       =   ProductEnum::$channel;;
        return $this->view('admin.product_import')->with(['product' => $product]);
    }

    /**
     * 产品入库
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ProductImportAction()
    {
        #收集信息
        $paramObj                       =       new stdClass();
        $paramObj->product_name         =       $this->getParam('product_name');
        $paramObj->product_sn           =       $this->getParam('product_sn',ToolKit::make_product_sn());
        $paramObj->stock                =       intval($this->getParam('stock'));
        $paramObj->brand_id             =       $this->getParam('brand_id');
        $paramObj->quality              =       $this->getParam('quality');
        $paramObj->price                =       $this->getParam('price');
        $paramObj->unit                 =       $this->getParam('unit');
        $paramObj->brief                =       $this->getParam('brief');
        $paramObj->description          =       $this->getParam('description');
        $paramObj->place                =       $this->getParam('place');
        $paramObj->spec                 =       $this->getParam('spec');
        $paramObj->level                =       $this->getParam('level');
        $paramObj->storage              =       $this->getParam('storage');
        $paramObj->start_num            =       $this->getParam('start_num');
        $paramObj->channel_id           =       $this->getParam('channel_id');
        $paramObj->is_index             =       $this->getParam('is_index');
        $paramObj->is_up                =       $this->getParam('is_up');
        $paramObj->supplier_id          =       1;
        $paramObj->product_img          =       $this->getParam('product_img');
        $paramObj->show_img1            =       $this->getParam('show_img1');
        $paramObj->show_img2            =       $this->getParam('show_img2');
        $paramObj->show_img3            =       $this->getParam('show_img3');

        #逻辑处理
        try{
            AdminService::instance()->ImportProduct($paramObj);
            return Redirect::to('/manage_product');
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 产地录入
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ProductPlaceImport()
    {
        return $this->view('admin.product_place_import');
    }

    /**
     * 产地入库
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ProductPlaceImportAction()
    {
        #收集信息
        $paramObj                       =       new stdClass();
        $paramObj->place_name           =       $this->getParam('place_name');
        $paramObj->is_foreign           =       $this->getParam('is_foreign');

        #逻辑处理
        try{
            AdminService::instance()->ImportProductPlace($paramObj);
            return Redirect::to('/product_place');
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 品牌录入
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ProductBrandImport()
    {
        $quality       =   GoodsService::instance()->GetProductCateList();   //材质
        return $this->view('admin.product_brand_import')->with(['quality' => $quality]);
    }

    /**
     * 品牌入库
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ProductBrandImportAction()
    {
        #收集信息
        $paramObj                       =       new stdClass();
        $paramObj->brand_name           =       $this->getParam('brand_name');
        $paramObj->quality              =       $this->getParam('quality');
        $paramObj->brand_img            =       $this->getParam('brand_img');
        $paramObj->is_show              =       $this->getParam('is_show');

        #逻辑处理
        try{
            AdminService::instance()->ImportProductBrand($paramObj);
            return Redirect::to('/product_brand');
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 分类录入
     * @return mixed
     * @author buluota
     * @date 2016-03-06
     */
    public function ProductCateImport()
    {
        return $this->view('admin.product_cate_import');
    }

    /**
     * 品类录入
     * @return mixed
     * @author buluota
     * @date 2016-03-07
     */
    public function ProductClassImport()
    {
        return $this->view('admin.product_class_import');
    }

    /**
     * 品类录入
     * @return mixed
     * @author buluota
     * @date 2016-03-07
     */
    public function ProductQualityImport()
    {
        return $this->view('admin.product_quality_import');
    }

    /**
     * 等级录入
     * @return mixed
     * @author buluota
     * @date 2016-03-07
     */
    public function ProductLevelImport()
    {
        $quality       =   GoodsService::instance()->GetProductCateList();   //材质
        return $this->view('admin.product_level_import')->with(['quality' => $quality]);
    }

    /**
     * 等级入库
     * @return mixed
     * @author buluota
     * @date 2016-03-07
     */
    public function ProductLevelImportAction()
    {
        #收集信息
        $paramObj                       =       new stdClass();
        $paramObj->level_name           =       $this->getParam('level_name');
        $paramObj->quality              =       $this->getParam('quality');
        $paramObj->sort_by              =       $this->getParam('sort_by');

        #逻辑处理
        try{
            AdminService::instance()->ImportProductLevel($paramObj);
            return Redirect::to('/product_level_import');
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 规格录入
     * @return mixed
     * @author buluota
     * @date 2016-03-07
     */
    public function ProductSpecImport()
    {
        $quality       =   GoodsService::instance()->GetProductCateList();   //材质
        return $this->view('admin.product_spec_import')->with(['quality' => $quality]);
    }

    /**
     * 等级入库
     * @return mixed
     * @author buluota
     * @date 2016-03-07
     */
    public function ProductSpecImportAction()
    {
        #收集信息
        $paramObj                       =       new stdClass();
        $paramObj->spec_name            =       $this->getParam('spec_name');
        $paramObj->quality              =       $this->getParam('quality');
        $paramObj->sort_by              =       $this->getParam('sort_by');

        #逻辑处理
        try{
            AdminService::instance()->ImportProductSpec($paramObj);
            return Redirect::to('/product_spec_import');
        }catch(Exception $e){
            $this->rest->error($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取等级信息
     * @return mixed
     * @author buluota
     * @date 2016-03-07
     */
    public function GetLevelInfo()
    {
        #收集信息
        $paramObj                       =       new stdClass();
        $paramObj->quality              =       $this->getParam('quality');

        #逻辑处理
        $data = AdminService::instance()->GetLevelInfo($paramObj->quality);
        if(is_array($data)){
            $this->rest->success($data,ErrorCodeEnum::STATUS_SUCCESS,"获取等级信息成功!");
        }else{
            $this->rest->error("获取等级信息成功!",ErrorCodeEnum::STATUS_ERROR, (array)$paramObj);
        }
    }

    /**
     * 获取规格信息
     * @return mixed
     * @author buluota
     * @date 2016-03-07
     */
    public function GetSpecInfo()
    {
        #收集信息
        $paramObj                       =       new stdClass();
        $paramObj->quality              =       $this->getParam('quality');

        #逻辑处理
        $data = AdminService::instance()->GetSpecInfo($paramObj->quality);
        if(is_array($data)){
            $this->rest->success($data,ErrorCodeEnum::STATUS_SUCCESS,"获取规格信息成功!");
        }else{
            $this->rest->error("获取规格信息成功!",ErrorCodeEnum::STATUS_ERROR, (array)$paramObj);
        }
    }


//////////////////////////弃用函数////////////////////////////////////
    /**
     * @return AdminOne
     */
    public function getAdminOne($id)
    {
        return $this->mAdmin->getAdminOneInfo($id);
    }

    /**
     *修改用户信息
     * @return
     */
    public function EditAdmin()
    {

        if(Request::isMethod('POST'))
        {
            #接受参数
            $aData              =   new stdClass();
            $aData->id          =   $this->getParam('id');
            $aData->name        =   $this->getParam('name');
            $aData->real_name   =   $this->getParam('real_name');
            $aData->password    =   $this->getparam('password');
            $aData->avatar      =   $this->getParam('avatar');
            $aData->role_id     =   $this->getParam('role_id');
            $aData->mobile      =   $this->getparam('mobile');
            $aData->status      =   $this->getparam('status');
            $aData->created     =   time();
            $check =  $this->checkProperties($aData,['name','real_name','avatar','role_id','password','mobile','status','created']);
            if($check){
                $this->rest->error('传参为空!',ErrorCodeEnum::STATUS_ERROR,$aData);}
            AdminService::instance()->editAdminInfo($aData,$aData->id);
            $this->rest->success([],ErrorCodeEnum::STATUS_SUCCESS,'成功啦!');}else{
            $id = $this->getParam('id');
            $info = AdminService::instance()->getAdminOne($id);
            return $this->view('admin.edit_admin')->with('admin',$info);
        }

    }


    /**
     * 表单数据验证
     * @param $obj
     * @param $arr
     * @return bool
     */
    public function  checkProperties($obj, $arr)
    {
        if(empty($arr)) return true;
        $obj    =   (array) $obj;
        foreach($arr as $v){
            if(empty($obj[$v])){
                return false;}
        }
        return true;
    }
}
